<template>
  <div>
      <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col>
          <!-- 添加角色按钮 -->
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表区域 -->
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template v-slot="slotProps">
            <el-row v-for=" (item, i1) in slotProps.row.children " :key="item.id" :class=" ['bdBottom', i1 === 0? 'bdTop' : '', 'vCenter'] ">
               <!-- 一级权限渲染区域 -->
              <el-col :span="6">
                <el-tag  closable @close="removeRigthsById(slotProps.row, item.id)">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级,三级权限权限渲染区域 -->
              <el-col :span="18">
                <el-row  v-for="(item2, i2) in item.children" :key="item2.id" :class=" [i2 === 0 ? '' : 'bdTop', 'vCenter'] ">
                  <!-- 二级权限渲染区域 -->
                  <el-col :span="6">
                    <el-tag type="success"  closable @close="removeRigthsById(slotProps.row, item2.id)">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限渲染区域 -->
                  <el-col :span="18" >
                    <el-tag type="warning" v-for="(item3) in item2.children" :key="item3.id" closable @close="removeRigthsById(slotProps.row, item3.id)">{{ item3.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>
              {{ slotProps.row }}
            </pre> -->
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column  label="角色名称" prop="roleName"></el-table-column>
        <el-table-column  label="角色秒速" prop="roleDesc"></el-table-column>
        <el-table-column  label="操作"  width="300px">
          <template v-slot="slotProps">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRigthsDailog(slotProps.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 树形控件对话框 -->
    <el-dialog
      title="分配角色"
      :visible.sync="setRigthsDialogVisible"
      width="50%" @close="setRigthsDialogClosed">
      <!-- 属性控件区域 -->
      <el-tree :data="treeRoleList" :props="treeProps" default-expand-all node-key="id" show-checkbox :default-checked-keys="checkedKeys" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRigthsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRigths()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    return {
      roleList: [], // 角色列表数据名
      setRigthsDialogVisible: false, // 控制树形控件对话框的显示隐藏
      treeProps: { // 树形控件匹配规则
        children: 'children',
        label: 'authName'
      },
      checkedKeys: [], // 树形控件默认选中的数组
      treeRoleList: [], // 树形控件需要的角色列表数据
      roleId: '' // 分配权限功能要用到的角色id
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () { // 获取角色列表
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return // this.$msg.error('获取角色列表失败！')
      this.roleList = res.data
      console.log(this.roleList)
    },
    removeRigthsById (role, rigthsId) { // 根据id删除对应权限
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rigthsId}`)
        if (res.meta.status !== 200) return this.$msg.error('删除权限失败！')
        role.children = res.data
        this.$msg({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$msg({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async showSetRigthsDailog (role) { // 点击分配角色按钮显示树形控件对话框
      const { data: res } = await this.$http.get('rights/tree')
      this.roleId = role.id
      if (res.meta.status !== 200) return
      this.treeRoleList = res.data
      // 调用获取树形控件默认选中函数
      this.getCheckedKeys(role, this.checkedKeys)

      this.setRigthsDialogVisible = true
    },
    getCheckedKeys (node, arr) { // 获取树形控件默认选中的商品权限 函数
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item =>
        this.getCheckedKeys(item, arr)
      )
    },
    setRigthsDialogClosed () { // 监听树形控件关闭事件，重置树形控件默认选中节点
      this.checkedKeys = []
    },
    async setRigths () { // 点击确定按钮分配权限
      const arr1 = [...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()]
      const rid = arr1.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: rid })
      if (res.meta.status !== 200) return this.$msg.error('分配权限失败！')
      this.$msg.success('分配成功！')
      this.getRolesList()
      this.setRigthsDialogVisible = false
    }

  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdTop {
  border-top: 1px #ccc solid;
}
.bdBottom {
  border-bottom: 1px #ccc solid;
}
.vCenter {
  display: flex;
  align-items: center;
}
</style>
